package java_thinking.unit_21._21_2;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2019/5/30 10:16
 */

public class TestBlockInteger {
    public static void main(String[] args) {
        TestBlockInteger test = new TestBlockInteger();
        MyIntTest m1 = test.new MyIntTest("1");
        m1.setName("1");
        MyIntTest m2 = test.new MyIntTest("3");
        m2.setName("2");
        m1.start();
        m2.start();
    }

    /**
     * 需要同步的变量
     */
    private String s = "a";
    class MyIntTest extends Thread {
        /**
         * 局部变量
         */
        private Object o;
        private MyIntTest(Object o) {
            this.o = o;
        }

        @Override
        public void run() {

            synchronized (o) {
                System.out.println("进入同步代码块" + o.hashCode());
                if ("2".equals(TestBlockObject.MyTest.currentThread().getName())) {
                    s = "2";
                    System.out.println("got Thread 2 s hashCode" + s.hashCode());
                }
                for (int i = 0; i < 10; i++) {
                    System.out.println("线程:"+ TestBlockObject.MyTest.currentThread().getName()+" change a ==" +
                            + i + " o hashCode:" + o.hashCode() + " s hashCode: " + s.hashCode());
                }
            }
        }
    }
}
